  layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
      var form = layui.form;
      var table = layui.table;

      $(document).ready(function (){
          if('WebSocket' in window){
              //本地调试  ws://localhost:8080/boot/webSocket
              //生产地址ws://192.168.2.35:8080/boot/webSocket
              ws = new WebSocket('ws://localhost:8080/carTaskExecutionInfo');
              alarmPage.refreshInfo();

          }


          ws.onmessage = function(event){

              var res = $.parseJSON(event.data);
              if(res.code == 1){
                  //调用成功
                  var data = res.data;
                  for(var i = 0;i<data.length;i++){
                      var car = data[i];
                      document.getElementById("modelType"+car.no).value = car.modelType;
                      document.getElementById("status"+car.no).value = car.status;
                      document.getElementById("materialName"+car.no).value = car.materialName;
                      document.getElementById("tankCarNo"+car.no).value = car.tankCarNo;

                      document.getElementById("xIndexAct"+car.no).value = car.XIndexAct;
                      document.getElementById("xIndexTarget"+car.no).value = car.XIndexTarget;
                      document.getElementById("yIndexAct"+car.no).value = car.YIndexAct;
                      document.getElementById("yIndexTarget"+car.no).value = car.YIndexTarget;
                      document.getElementById("zIndexAct"+car.no).value = car.ZIndexAct;
                      document.getElementById("zIndexTarget"+car.no).value = car.ZIndexTarget;
                  }
              }
              if(res.code == 10){
                  //返回信息为异常报警信息更新
                  alarmPage.refreshInfo();
              }

              if(res.code == 20){
                  //返回信息为任务报错信息
                  layer.alert(res.data);
              }

              if(res.code == 30){
                  //返回信息为点位监控信息
                  var data = res.data;
                  layer.confirm(data+"设备出现异常。请问需要确定修复异常吗？",
                      {icon: 3,id:data,type:1},
                      function(){
                      //点击确认，修复异常  发送Ajax请求，复位异常点位
                      layer.msg('点击确定的回调', {icon: 1});
                      //弹出第二次按钮，确定是否需要继续执行任务
                      Page.resumeTask(data);
                  }, function(){
                      //点击取消的回调
                      layer.msg('已取消修复异常');
                  });
              }

          }

      });


      var Page = {
          tableId: 'sys-execution-table',
          url: CXL.ctxPath + '/sys/addTask/initTaskExecution',
          fixUrl:CXL.ctxPath + '/sys/addTask/fixAbnormal',
          resumeUrl:CXL.ctxPath + '/sys/addTask/resumeTask',
          initColumn:function(){
              return [[
                  {type : 'numbers',title : '序号',width:'3%'},
                  {field:'idKey',title:'idKey',hide:true,width:'1%'},
                  {field:'taskId',title:'任务号',width:'10%'},
                  {field:'materialName',title:'物料名称',width:'18%'},
                  {field:'materialNo',title:'物料编码',width:'13%'},
                  {field:'status',title:'任务状态',width:'15%',templet:"#execution-status"},
                  {field:'targetWeight',title:'目标重量',width:'18%'},
                  {field:'actualWeight',title:'实际重量',width:'18%'},
              ]];
          },
      };



      Page.resumeTask = function (data){
          var index = layer.confirm(data+"设备出现异常。请问需要继续该任务吗？", {icon: 3}, function(){
              //点击确认，继续执行任务   发送Ajax请求，复位任务暂停点位
              layer.close(index);
              var loadingIndex = layer.load();
              $.ajax({
                  type: "post",
                  url: Page.resumeUrl,
                  data: {carName:data},
                  success: function(res) {
                      if(res.success) {
                          CXL.success(res.msg);
                          layer.close(loadingIndex);

                      }else {
                          CXL.warn(res.msg);
                      }
                  }

              });
          }, function(){
              //点击取消的回调
              layer.msg('已取消继续执行任务');
          });
      }


      var alarmPage = {
          tableId: 'sys-alarm-table',
          url: CXL.ctxPath + '/sys/alarm/taskExecutionAlarmInfo',
          initColumn:function(){
              return [[
                  {type : 'numbers',title : '序号',width:'5%'},
                  {field:'carName',title:'报警位置',width:'20%'},
                  {field:'errMsg',title:'报警详情',width:'50%'},
                  {field:'createDate',title:'报警时间',width:'20%'}
              ]];
          },
      };


      // 设置定时器，每隔20秒刷新页面
      setInterval(function() {
          var tankCarNo = $('#tankCarNo option:selected').val();


          if(tankCarNo == null || tankCarNo == ''){

              return;
          }

          var param = {tankCarNo:tankCarNo};
          table.render({
              elem:'#' + Page.tableId,
              url:Page.url,
              height:'full-500',
              page:false,
              autoSort:false,
              where: param,
              cols:Page.initColumn(),
              done:function(res,curr,count){
                  var color = "#011d4d";
                  var size = "bold";
                  $('th').css("background-color",color);
                  $('th').css("font-weight",size);
                  $('tbody').css("background-color",color);
              }
          });
      }, 20000);





      //选择框联动
      form.on('select(select-tankCarNo)', function(data){
          var tankCarNo = $('#tankCarNo option:selected').val();
          var param = {tankCarNo:tankCarNo};
          table.render({
              elem:'#' + Page.tableId,
              url:Page.url,
              height:'full-500',
              page:false,
              autoSort:false,
              where: param,
              cols:Page.initColumn(),
              done:function(res,curr,count){
                  var color = "#011d4d";
                  var size = "bold";
                  $('th').css("background-color",color);
                  $('th').css("font-weight",size);
                  $('tbody').css("background-color",color);
              }
          });

      });


      alarmPage.refreshInfo = function (){
          table.render({
              elem:'#' + alarmPage.tableId,
              url:alarmPage.url,
              height:'full',
              page:true,
              limits:[3,5,7,10,13,20,30],
              limit:5,
              autoSort:false,
              cols:alarmPage.initColumn(),
              done:function(res,curr,count){
                  var color = "#011d4d";
                  var size = "bold";
                  $('th').css("background-color",color);
                  $('th').css("font-weight",size);
                  $('tbody').css("background-color",color);
              }
          });
      }













  })